package com.example.performance.jmh.official;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * Should your benchmark require returning multiple results, you have to
 * consider two options (detailed below).
 * <p>
 * NOTE: If you are only producing a single result, it is more readable to
 * use the implicit return, as in JMHSample_08_DeadCode.
 * Do not make your benchmark code less readable with explicit Blackholes!
 */
@SuppressWarnings("java:S101")
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 2, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class JMHSample_09_Blackholes {

    double x1 = Math.PI;
    double x2 = Math.PI * 2;

    private double compute(double d) {
        for (int c = 0; c < 10; c++) {
            d = d * d / Math.PI;
        }
        return d;
    }

    /*
     * Baseline measurement: how much a single compute() costs.
     */
    @Benchmark
    public double baseline() {
        return compute(x1);
    }

    /*
     * While the compute(x2) computation is intact, compute(x1)
     * is redundant and optimized out.
     */
    @Benchmark
    public double measureWrong() {
        compute(x1);
        return compute(x2);
    }

    /*
     * This demonstrates Option A:
     *
     * Merge multiple results into one and return it.
     * This is OK when is computation is relatively heavyweight,
     * and merging the results does not offset the results much.
     */
    @Benchmark
    public double measureRight1() {
        return compute(x1) + compute(x2);
    }

    /*
     * This demonstrates Option B:
     *
     * Use explicit Blackhole objects, and sink the values there.
     * (Background: Blackhole is just another @State object, bundled with JMH).
     */
    @Benchmark
    public void measureRight2(Blackhole bh) {
        bh.consume(compute(x1));
        bh.consume(compute(x2));
    }

    /**
     * ============================== HOW TO RUN THIS TEST: ====================================
     * <p>
     * You will see measureWrong() running on-par with baseline().
     * Both measureRight() are measuring twice the baseline, so the logs are intact.
     * <p>
     * You can run this test:
     * <p>
     * a) Via the command line:
     *    $ mvn clean install
     *    $ java -jar target/benchmarks.jar JMHSample_09 -f 1
     *    (we requested single fork; there are also other options, see -h)
     * <p>
     * b) Via the Java API:
     *    (see the JMH homepage for possible caveats when running from IDE:
     *      <a href="http://openjdk.java.net/projects/code-tools/jmh/">...</a>)
     */
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(JMHSample_09_Blackholes.class.getSimpleName())
                .forks(1)
                .build();

        new Runner(opt).run();
    }


}